﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using PMS.IRepository;
using PMS.IServices;
using PMS.Models.Base;
using PMS.Repository;
using PMS.Utils;

namespace PMS.Services
{
    public class BaseMenuService : IBaseMenuService
    {
        IBaseMenuRepository _baseMenuRepository = new BaseMenuRepository();

        #region 公共方法

        public int Add(BaseMenu user)
        {
            return _baseMenuRepository.Add(user);
        }

        public int Edit(BaseMenu user)
        {
            return _baseMenuRepository.Edit(user);
        }

        public BaseMenu Dtl(int id)
        {
            return _baseMenuRepository.Dtl(id);
        }

        public bool IsExists(int id)
        {
            return _baseMenuRepository.IsExists(id);
        }

        public int Delete(int id, string deleteBy)
        {
            Hashtable ht = new Hashtable { { "ParentId", id } };
            List<int> Ids = _baseMenuRepository.GetList(ht)?.Select(x => x.Id).ToList();
            if (Ids != null)
            {
                _baseMenuRepository.Delete(Ids.ToArray(), deleteBy);
            }
            return _baseMenuRepository.Delete(id, deleteBy);
        }

        public int Delete(int[] ids, string deleteBy)
        {
            return _baseMenuRepository.Delete(ids, deleteBy);
        }

        public List<BaseMenu> GetList()
        {
            return _baseMenuRepository.GetList();
        }

        #endregion

        public List<BaseMenu> GetList(Hashtable ht)
        {
            return _baseMenuRepository.GetList(ht);
        }

        public int AddEditParent(int parentId)
        {
            BaseMenu menu = _baseMenuRepository.Dtl(parentId);
            if (menu == null || menu.IsLeaf == 1)
            {
                return 1;
            }
            menu.IsLeaf = 1;
            return _baseMenuRepository.Edit(menu);
        }

        public int DeleteEditParent(int parentId)
        {
            int counts = _baseMenuRepository.GetCount(new Hashtable { { "ParentId", parentId } });
            if (counts <= 0)
            {
                BaseMenu menu = _baseMenuRepository.Dtl(parentId);
                menu.IsLeaf = 0;
                return _baseMenuRepository.Edit(menu);
            }

            return 1;
        }
    }
}